package com.taxsystem.huangguangxing.taxrelatedcheck;

import com.main.taxsystem.R;
import java.util.ArrayList;

import java.util.concurrent.ExecutionException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.taxsystem.huangguangxing.commoncheck.Title;
import com.taxsystem.tool.HttUtil;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;

public class TaxrelatedcheckShowTitleActivity extends Activity implements
		SearchView.OnQueryTextListener {
	
	public final static String RECI_COAST = "com.taxsystem.huangguangxing.taxrelatedcheck.http_1";

	String url = "http://itaxmanagerapi.duapp.com/app/article/getarticlelist?arttypeid=1&fuzzyquery=abc&orderby=time";
	String stringTemp;

	ArrayList<Title> listTitles;
	ArrayList<Title> listTitlesTemp;
	ArrayAdapter<Title> arrayAdapter;
	ListView listView;
	SearchView searchView;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {


		super.onCreate(savedInstanceState);
		setContentView(R.layout.taxrelated_check_1);
		
		listView = (ListView) findViewById(R.id.listView1);
		searchView = (SearchView) findViewById(R.id.searchView1);
		
		IntentFilter filter = new IntentFilter(RECI_COAST);
		BroadcastReceiver myrecive = new MyRecive();
		registerReceiver(myrecive, filter);
		new Thread() {

			@Override
			public void run() {
				Intent intent = new Intent(RECI_COAST);
				try {
					// 获取服务器返回的信息
					String reslut = HttUtil
							.getRequest(url);
					intent.putExtra("taxrelateds", reslut);
					// 发送广播
					sendBroadcast(intent);
				} catch (InterruptedException e) {
					e.printStackTrace();
				} catch (ExecutionException e) {
					e.printStackTrace();
				}
			}

		}.start();

		
		listTitles = new ArrayList<Title>();
		listTitlesTemp = new ArrayList<Title>();

		arrayAdapter = new ArrayAdapter<Title>(this,
				android.R.layout.simple_list_item_1, listTitlesTemp);
				
		listView.setAdapter(arrayAdapter);
		listView.setTextFilterEnabled(true);
		searchView.setOnQueryTextListener(this);
		searchView.setSubmitButtonEnabled(false);
		// 监听
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {

				int item = arg2;
				int n_id = listTitlesTemp.get(item).getArtId();
				tendto(n_id);
			}

		});
	}

	private class MyRecive extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			try {
				//获取JSONObject对象
				JSONObject object = new JSONObject(
						intent.getStringExtra("taxrelateds"));
				JSONArray array = new JSONArray(object.getString("data"));

				for (int i = 0; i < array.length(); i++) {
					JSONObject jsonObject = array.getJSONObject(i);
					Title title = new Title();
					title.setArtId(jsonObject.getInt("artId"));
					title.setTitle(jsonObject.getString("title"));
					title.setCreateTime(jsonObject.getString("createTime"));
					listTitles.add(title);
					listTitlesTemp.add(title);
					arrayAdapter.notifyDataSetChanged();

				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}

	}
	
	// 根据点击的标题转向具体的文章
	public void tendto(int m) {
		Intent intent = new Intent();
		/* 指定intent要启动的类 */
		intent.setClass(TaxrelatedcheckShowTitleActivity.this, TaxrelatedcheckShowArticleActivity.class);
		Bundle bundle = new Bundle();
		bundle.putString("taxrelatedcheck", m + "");
		intent.putExtras(bundle);
		/* 启动一个新的Activity */
		startActivity(intent);
	}

	@Override
	public boolean onQueryTextChange(String newText) {
		// TODO Auto-generated method stub
		searchItem(newText);
		updateLayout();
		return false;
	}

	@Override
	public boolean onQueryTextSubmit(String query) {
		// TODO Auto-generated method stub
		return false;
	}

	// 匹配关键字
	public void searchItem(String name) {

		listTitlesTemp.clear();
		ArrayList<String> mSearchList = new ArrayList<String>();
		for (int i = 0; i < listTitles.size(); i++) {
			int index = listTitles.get(i).getTitle().indexOf(name);
			// 存在匹配的数据
			if (index != -1) {
				mSearchList.add(listTitles.get(i).toString());
				listTitlesTemp.add(listTitles.get(i));
			}
		}
		//return mSearchList.toArray();
	}

	// 更新listView
	public void updateLayout() {
//		listView.setAdapter(new ArrayAdapter<Object>(getApplicationContext(),
//				android.R.layout.simple_expandable_list_item_1, obj));
		arrayAdapter.notifyDataSetChanged();
	}

}
